package com.fs.mjx.service.impl;

import com.fs.mjx.Dao.UserDao;
import com.fs.mjx.Dao.UserRoleDao;
import com.fs.mjx.pojo.Role;
import com.fs.mjx.pojo.User;
import com.fs.mjx.pojo.UserRole;
import com.fs.mjx.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @description:
 * @author： mjx
 * @createDate: 2020/9/15
 * @version: 1.0
 */
//IOC容器帮我们去new这个实现对象
//    IOC容器帮我们去new这个实现对象
@Service
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    private UserRoleDao userRoleDao;
    @Autowired//自动装配
    public UserServiceImpl(UserDao userDao, UserRoleDao userRoleDao) {
        this.userDao = userDao;
        this.userRoleDao = userRoleDao;
    }

    @Override
    public List<User> getAll() {
        return userDao.queryAll();
    }

    @Override
    public User getOneByUserId(Integer userId) {
        User user = userDao.queryById(userId);
        System.out.println(user);
        return user;
    }

    @Override
    public PageInfo<User> getUsersByPageForQuery(Integer currentPage, Integer pageSize,String address) {
        //准备拦截mybatis的后续查询
        PageHelper.startPage(currentPage,pageSize);
        List<User> userList =userDao.queryByColumn(address);
//        System.out.println(userList.size());
        //pagehelper内部帮我们计算总共有多少页
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    @Override
    public PageInfo<User> getUsersByPage(Integer currentPage, Integer pageSize) {
        //准备拦截mybatis的后续查询
        List<User> userList;
        PageHelper.startPage(currentPage,pageSize);
             userList = userDao.queryAll();
//        System.out.println(userList.size());
        //pagehelper内部帮我们计算总共有多少页
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    @Override
    public boolean addOne(User user) {
        int result = userDao.insertByUser(user);
        if(user.getRoles()!=null){
            for(Role role:user.getRoles()){
                UserRole userRole=new UserRole();
                userRole.setRoleId(role.getRoleId());
                userRole.setUserId(user.getUserId());
                userRoleDao.insert(userRole);
            }
        }
        return result>0;
    }


    @Override
    public boolean removeOne(User user) {
        int result = userDao.deleteByUser(user);
        return result>0;
    }

    @Override
    public boolean modifyOne(User user) {
        int result = userDao.updateByUser(user);
        return result>0;
    }

    @Override
    public boolean removeByUserId(int UserId) {
        int result = userDao.deleteById(UserId);
        return result>0;
    }

    @Override
    public boolean removeSome(Set<Integer> userIds) {
        int result = userDao.deleteByIds(userIds);
        return result>0;
    }

    @Override
    public List<User> getUserThatAddressLike(String address) {
        return userDao.queryByColumn(address);
    }

    @Override
    public Set<User> getUserWithMultiCondition(String userName, String userPhone, String roleName) {
        return userDao.queryByMultiConditions(userName,userPhone,roleName);
    }
}
